package com.xci.platform.helper;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;

import java.util.Map;

public class MapHelper {
    /**
     * 去除map中值得空格
     *
     * @param map 数据
     */
    public static void trimMapValue(Map<String, String> map) {
        for (Map.Entry<String, String> item : map.entrySet()) {
            String value = item.getValue();
            if (value != null) {
                item.setValue(value.trim());
            }
        }
    }

    /**
     * 设置日期查询条件
     *
     * @param params 查询对象
     */
    @SuppressWarnings("unchecked")
    public static void setQueryDateTimeParam(Map params) {
        String startKey = "operateStartDateTime";
        String endKey = "operateEndDateTime";
        setQueryDateTimeParam(params, startKey, endKey);
    }

    /**
     * 设置日期查询条件
     *
     * @param params 查询对象
     */
    @SuppressWarnings("unchecked")
    public static void setQueryDateTimeParam(Map params, String startKey, String endKey) {
        Object startValue = params.get(startKey);
        Object endValue = params.get(endKey);
        if (ObjectHelper.isNotBlank(startValue)) {
            params.put(startKey, DateUtil.parse(startValue.toString()).toJdkDate());
        } else {
            params.remove(startKey);
        }
        if (ObjectHelper.isNotBlank(endValue)) {
            params.put(endKey, DateUtil.parse(endValue.toString()).offset(DateField.DAY_OF_MONTH, 1).toJdkDate());
        } else {
            params.remove(endKey);
        }
    }
}
